<script type="text/html" template lay-done="layui.data.temporaryPastdone(d);">
   <div class='temporary-pastHtml-content'>
        <div class="layui-row">
            <div class="layui-col-md12">
                <div class="layui-card-header">
                    选择单位：
                </div>
                <div class="ztreeContainer">
                    <ul id='temporary-pastHtml-ztree-department' class="ztree"></ul>
                </div>
                <div class="department-check">
                    <div class="check-value-box"></div>
                </div>
            </div>
        </div>

        <div class="layui-row" style="text-align: center;margin-top: 15px;">
            <textarea class="data-dep-container" style="display: none;" name="" id="" cols="30" rows="10">
                    {{#
                        if(d.params.exitDep) {
                    }}
                            {{JSON.stringify(d.params.exitDep)}}
                    {{#
                        }else {
                    }}
                            []
                    {{#
                        }
                    }}
            </textarea>
            <button type="button" class="layui-btn T-confirm" data-groupstr="" data-depstr="">确认</button>
        </div>
    </div> 
</script>
<style>
    .temporary-pastHtml-content {
        padding: 0px 10px;
    }
    .layui-layer-page .layui-layer-content{
        background-color: #FFFFFF;
    }
    .temporary-pastHtml-content .ztreeContainer {
        height: 400px;
        overflow-y: auto;
    }

    .temporary-pastHtml-content .department-check,
    .temporary-pastHtml-content .group-check {
        height: 70px;
        border: 1px solid #ccc;
        margin: 15px 0px 0px 13px;
        padding: 2px 2px;
    }

    .temporary-pastHtml-content .check-value-box {
        width: 100%;
        height: 100%;
        overflow-y: auto;
    }
</style>
<script>
    layui.data.temporaryPastdone = function (d) {
        layui.use(['admin'], function () {
            var admin = layui.admin;
            var exitDep = d.params.exitDep ?  d.params.exitDep : [];
            var taskId = d.params.taskid ? d.params.taskid : '';
            var checkDepartment = exitDep;

            var zTreeObjDep, zTreeObjGroup;
            var departmentSetting = {
                check: {
                    enable: true,
                    chkStyle: "checkbox",
                    chkboxType: {
                        "Y": "",
                        "N": ""
                    }
                },
                data: {
                    key: {
                        name: "dept_name",
                    },
                    simpleData: {
                        idKey: "id",
                        pIdKey: "pId",
                        rootPId: 0
                    }
                },
                async: {
                    enable: true,
                    type: 'get',
                    url: "myapi/DC/api/rest/web/custom/GetDeptInfo?access_token="+layui.data('layuiAdmin').access_token+"&taskid="+taskId+"&jsonId="+layui.data('layuiAdmin').access_token,
                    dataFilter: ajaxDataFilter,
                    autoParam: ["id"]
                },
                callback: {
                    onCheck: departmentCheck
                }
            };

            getDept(taskId);

            //过滤节点返回的数据
            function ajaxDataFilter(treeId, parentNode, responseData) {
                var nodesData = [];
                if (responseData.retcode === '0' && responseData.data && responseData.data.length >0) {
                    var allArr = exitDep;
                    $.each(responseData.data, function (index, item) {
                        $.each(allArr, function(i,t) {
                            if(t.dept_id === item.id) {
                                item.checked=true;
                            }
                        })
                        item.isParent = true;
                        nodesData.push(item);
                    })
                    nodesData = responseData.data;
                }
                return nodesData;
            }

            //单位选择
            function departmentCheck(event, treeId, treeNode) {
                $container = $('.temporary-pastHtml-content');
                var flag = false,
                    dealDepartment = [];
                for (var i = 0; i < checkDepartment.length; i++) {
                    if (checkDepartment[i].dept_id === treeNode.id) {
                        flag = true;
                    } else {
                        dealDepartment.push(checkDepartment[i]);
                    }
                }
                if (!flag) {
                    dealDepartment.push({
                        dept_name: treeNode.dept_name,
                        dept_id: treeNode.id,
                        dataid: treeNode.id,
                        dept_code: treeNode.dept_code,
                        writeusername: treeNode.dept_name
                    });
                }
                var nameStr = [];
                $.each(dealDepartment, function (index, item) {
                    nameStr.push(item.writeusername)
                });
                checkDepartment = dealDepartment;
                $container.find('.department-check .check-value-box').text(nameStr.join());
                $container.find('.data-dep-container').val(JSON.stringify(dealDepartment));
            }
            
            function getDept(taskid){
                var params = {
                    jsonId: layui.data('layuiAdmin').access_token,
                    taskid: taskid
                };
                admin.req({
                    url: 'myapi/DC/api/rest/web/custom/GetDeptInfo',
                    data: params,
                    success: function (res) {
                        var dataArr = [];
                        if(res.retcode == '0'){
                            $.each(res.data, function (index, item) {
                                $.each(exitDep, function(i,t) {
                                    if(t.dataid === item.id) {
                                        item.checked=true;
                                    }
                                })
                                item.isParent = true;
                                dataArr.push(item);
                            })
                            zTreeObjDep = $.fn.zTree.init($("#temporary-pastHtml-ztree-department"),
                                departmentSetting, res.data);
                        }else if(res.retcode == '-2'){
                            location.hash = '/user/login'
                        }
                    }
                });
            }
        })
    }
</script>